<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Config extends Controller_Admin_Based
{

    public function action_list()
    {
        Session::instance()->set('here', 'here');
        $pagination_arr = Paginationhelp::get_data('config', 'id_config');

        $content_data = array(
            'result' => $pagination_arr['data'],
            'page_view' => $pagination_arr['pagination']
        );
        $view = Viewer::display('admin/system/config_list', $content_data);
        $this->response->body($view);
    }

    public function action_add()
    {

        if ($_POST)
        {
            $config = ORM::factory('config');
            $config_exits = $config->where('fieldname', '=', $_POST['fieldname'])->count_all();
            if ($config_exits)
            {
                Message::set('变量添加失败,变量已存在！', 'error');
                Request::current()->redirect('/admin/config/add');
            }
            else
            {
                $config = Config::instances()->set($_POST);
                if ($config)
                {
                    Message::set('变量添加成功！');
                    Request::current()->redirect('/admin/config/add');
                }
                else
                {
                    Message::set('变量添加失败！', 'error');
                    Request::current()->redirect('/admin/config/add');
                }
            }
        }

        $pagination_arr = Paginationhelp::get_data('config', 'id_config');
        $data = array(
            'result' => $pagination_arr['data'],
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/config_add', $data);
        $this->response->body($view);
    }

    public function action_edit()
    {
        $id_config = $this->request->param('id');

        $config = Config::instances($id_config)->get();
        if ($_POST)
        {
            $config = Config::instances($id_config)->update($_POST);
            if ($config)
            {
                Message::set('变量添加成功！');
                Request::current()->redirect('/admin/config/edit/' . $id_config);
            }
            else
            {
                Message::set('变量添加失败！', 'error');
                Request::current()->redirect('/admin/config/edit/' . $id_config);
            }
        }

        
         $pagination_arr = Paginationhelp::get_data('config', 'id_config');
        $data = array(
            'result' => $pagination_arr['data'],
            'data' => $config,
            'page_view' => $pagination_arr['pagination']
        );

        $view = Viewer::display('admin/config_edit', $data);
        $this->response->body($view);
    }

    public function action_delete()
    {

        $id_config = $this->request->param('id');

        $flag = Config::instances($id_config)->delete();

        Message::set('参数删除成功');
        Request::current()->redirect('/admin/config/add');
    }

}